package com.lagou.module2homewor;

import org.jetbrains.annotations.NotNull;

import java.util.Scanner;

/**
 * 五子棋游戏类
 */
public class FiveInARowGame {

    private ChessBoard chessBoard;      //棋盘
    private int posX;                   //用户输入的X坐标
    private int posY;                   //用户输入的Y坐标
    private final static int WIN_COUNT=5;   //判断赢的棋子数

    /**
     * 空构造器
     */
    public FiveInARowGame() { }

    /**
     * 参数构造器
     * @param chessBoard 棋盘信息
     */
    public FiveInARowGame(ChessBoard chessBoard) {
        this.chessBoard = chessBoard;
    }

    public int getPosX() {
        return posX;
    }

    public void setPosX(int posX) {
        this.posX = posX;
    }

    public int getPosY() {
        return posY;
    }

    public void setPosY(int posY) {
        this.posY = posY;
    }

    /**
     * 判断用户输入的是否合法
     * @param inputStr 用户输入的坐标信息
     * @return  true-合法，false-不合法
     */
    public boolean isValid(@NotNull String inputStr){
        //将用户输入的字符串使用英文逗号，分隔成一个字符串数组，分别储存X和Y坐标
        String[] inputStrArr = inputStr.split(",");
        //将字符串类型的坐标转换成Int型的坐标
        try {
            setPosX(Integer.parseInt(inputStrArr[0]));
            setPosY(Integer.parseInt(inputStrArr[1]));
        }catch (NumberFormatException e){
            chessBoard.drawChessBorad();
            System.out.println("请按照(数字，数字)的格式输入： ");
            return false;
        }

        //判断用户输入的坐标是否在棋盘的返回内
        if( (posX < 0 || posX > ChessBoard.BOARD_SIZE) || (posY < 0 || posY > ChessBoard.BOARD_SIZE)){
            chessBoard.drawChessBorad();
            System.out.println("输入的不合法，坐标只能在0到"+ChessBoard.BOARD_SIZE+"之间");
            return false;
        }

        //获取当前棋盘的状态
        String[][] board = chessBoard.getChessBoard();
        if(board[posX][posY] != "+"){
            System.out.println("当前位置已经有了棋子，请输入其他坐标");
            return false;
        }

        return true;
    }

    /**
     * 判断是否赢棋
     * @param posX x坐标
     * @param posY y坐标
     * @return true-赢 ，false-输
     */
    public boolean isWin(int posX,int posY,String chessman){

        int startX = 0;                         //开始的X坐标
        int startY = 0;                         //开始的Y坐标
        int endX = ChessBoard.BOARD_SIZE - 1;   //结束的X坐标
        int endY = ChessBoard.BOARD_SIZE - 1;   //结束的Y坐标

        //需要判断的坐标区间
        startX = (posX - WIN_COUNT) + 1 < 0 ? 0 : (posX - WIN_COUNT) + 1;
        startY = (posY - WIN_COUNT) + 1 < 0 ? 0 : (posY - WIN_COUNT) + 1;
        endX = (posX + WIN_COUNT) - 1 > endX ? endX : (posX + WIN_COUNT) - 1;
        endY = (posY + WIN_COUNT) -1 > endY ? endY : (posY + WIN_COUNT) - 1;

        int sameCount = 0;                      //区间内相同的棋子的个数
        String[][] board = chessBoard.getChessBoard();  //获得棋盘的状态
        //从左到右相同棋子的个数
        for(int j = startY; j < endY; j++){
            if(board[posX][j] == chessman && board[posX][j+1] == chessman){
                sameCount += 1;
            }
        }

        if(sameCount != WIN_COUNT - 1){
            sameCount = 0;
        }else {
            return true;
        }

        //从上到下相同棋子的个数
        for(int i = startX; i < endX; i++){
            if(board[i][posY] == chessman && board[i+1][posY] == chessman){
                sameCount += 1;
            }
        }

        if(sameCount != WIN_COUNT - 1){
            sameCount = 0;
        }else {
            return true;
        }

        //4条斜线判断
        int sum1 = 0;
        int sum2 = 0;
        int sum3 = 0;
        int sum4 = 0;
        //System.out.println(" startX = " + startX + " endX " + endX);
        //System.out.println(" startY = " + startY + " endY " + endY);
        if( startX == 0 ){
            startX = 1;
        }
        if( startY == 0){
            startY = 1;
        }
        for( int i = startX; i < endX; i++ ){
            for(int j = startY; j < endY; j++){
                if( board[i-1][j-1] == chessman && board[i][j] == chessman ){
                    sum1 += 1;
                }
                if( board[i-1][j+1] == chessman && board[i][j] == chessman){
                    sum2 += 1;
                }
                if ( board[i+1][j-1] == chessman && board[i][j] == chessman){
                    sum3 += 1;
                }
                if ( board[i+1][j+1] == chessman && board[i][j] == chessman){
                    sum4 += 1;
                }

            }
        }
        //System.out.println("  -----  " + sum1 + " " + sum2 + " " + sum3 + " " + sum4);

        if(sum1 == WIN_COUNT - 1 || sum2 == WIN_COUNT - 1 || sum3 == WIN_COUNT - 1 || sum4 == WIN_COUNT - 1 ){
           return true;
        }
        return false;
    }

    /**
     * 开始游戏
     */
    public void startGame(){
        chessBoard.initChessBorad();    //初始化棋盘

        while(true){
            String whiteStr = null;         //白方输入的坐标
            String blackStr = null;         //黑方输入的坐标
            chessBoard.drawChessBorad();    //输出棋盘

            Scanner sc = new Scanner(System.in);
            System.out.println("请黑方输入坐标(数字，数字):");  //提示用户输入
            blackStr = sc.nextLine();
            //输入合法，修改棋盘信息
            while(!isValid(blackStr)) {
                System.out.println("请黑方输入坐标(数字，数字):");
                blackStr = sc.nextLine();
            }
            chessBoard.setChessBorad(getPosX(),getPosY(),Chessman.BLACK.getChessman());
            chessBoard.drawChessBorad();    //输出棋盘
            //判断是不是赢了
            if(isWin(getPosX(),getPosY(),Chessman.BLACK.getChessman())){
                System.out.println("恭喜黑方赢棋!!!,游戏结束");
                break;
            }

            System.out.println("请白方输入坐标(数字，数字):");
            whiteStr = sc.nextLine();
            while(!isValid(whiteStr)){
                System.out.println("请白方输入坐标(数字，数字):");
                whiteStr = sc.nextLine();
            }
            //输入合法，修改棋盘信息
            chessBoard.setChessBorad(getPosX(),getPosY(),Chessman.WHITE.getChessman());
            chessBoard.drawChessBorad();    //输出棋盘
            //判断是不是赢了
            if(isWin(getPosX(),getPosY(),Chessman.WHITE.getChessman())){
                System.out.println("恭喜白方赢棋!!!,游戏结束");
                break;
            }
        }

    }

    public static void main(String[] args) {
        FiveInARowGame fiveInARowGame = new FiveInARowGame(new ChessBoard());
        fiveInARowGame.startGame();
    }
}
